package at.dl.android.dota.adviser;

import java.util.List;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Spinner;

public class SearchHeroActivity extends AbstractSearchActivity {

    public final static String SEARCH_HERO_WHERE_CAUSE = "SEARCH_HERO_WHERE_CAUSE";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,
                       R.layout.search_hero,
                       R.id.buttonSearch,
                       ListHeroActivity.class,
                       SEARCH_HERO_WHERE_CAUSE,
                       "HEROES",
                       "name1");
    }

    @Override
    protected void fillWhereElements ( List<String> whereElements ) {
        handleName(whereElements);
        handleRange(whereElements);
        handlePrimeAttr(whereElements);
        handleLane(whereElements);
        handleRoles(whereElements);
        handleFeatures(whereElements);
        handleRestrictions(whereElements);
    }

    private void handleName ( List<String> whereElements ) {
        EditText et = (EditText) findViewById(R.id.editName);
        String searchName = et.getText().toString().trim();
        if ( searchName.length() <= 0 )
            return;
        if ( searchName.charAt(searchName.length()-1) != '%' )
            searchName += '%';
        String tmp = " like '" + searchName + "'";
        String x = "(name1" + tmp 
                 + " OR name2" + tmp
                 + " OR nickname1" + tmp
                 + " OR nickname2" + tmp + ")";
        whereElements.add(x);        
    }

    private void handlePrimeAttr ( List<String> whereElements ) {
        Spinner sp = (Spinner) findViewById(R.id.spinnerPrimAttr);
        String sel = sp.getSelectedItem().toString();
        if ( "(all)".equals(sel) )
            return;
        whereElements.add("primaty_att = '" + sel + "'");        
    }

    private void handleRange ( List<String> whereElements ) {
        Spinner sp = (Spinner) findViewById(R.id.spinnerRange);
        String sel = sp.getSelectedItem().toString();
        if ( "(all)".equals(sel) )
            return;
        else if ( "Melee".equals(sel) )
            whereElements.add("range <= 150");
        else
            whereElements.add("range > 150");        
    }

    private void handleLane ( List<String> whereElements ) {
        Spinner sp = (Spinner) findViewById(R.id.spinnerLane);
        String sel = sp.getSelectedItem().toString();
        if ( "(all)".equals(sel) )
            return;
        whereElements.add(sel.toLowerCase() + " = 1");        
    }

    private void handleRoles ( List<String> whereElements ) {
        addFromCheckBox(whereElements,"carry",R.id.checkBoxCarry);
        addFromCheckBox(whereElements,"semi_carry",R.id.checkBoxSemiCarry);
        addFromCheckBox(whereElements,"ganker",R.id.checkBoxGanker);
        addFromCheckBox(whereElements,"hunter",R.id.checkBoxHunter);
        addFromCheckBox(whereElements,"supporter",R.id.checkBoxSupporter);
        addFromCheckBox(whereElements,"tank",R.id.checkBoxTank);
        addFromCheckBox(whereElements,"ward_bitch",R.id.checkBoxWardBitch);
        addFromCheckBox(whereElements,"disabler",R.id.checkBoxDisabler);
        addFromCheckBox(whereElements,"initiator",R.id.checkBoxInitiator);
        addFromCheckBox(whereElements,"pusher",R.id.checkBoxPusher);
        addFromCheckBox(whereElements,"anti_pusher",R.id.checkBoxAntiPusher);
    }
    
    private void handleFeatures ( List<String> whereElements ) {
        addFromCheckBox(whereElements,"bash",R.id.checkBoxBash);
        addFromCheckBox(whereElements,"stun",R.id.checkBoxStun);
        addFromCheckBox(whereElements,"aoe_stun",R.id.checkBoxAoeStun);
        addFromCheckBox(whereElements,"mini_stun",R.id.checkBoxMiniStun);
        addFromCheckBox(whereElements,"slow",R.id.checkBoxSlow);
        addFromCheckBox(whereElements,"aoe_slow",R.id.checkBoxAoeSlow);
        addFromCheckBox(whereElements,"hold",R.id.checkBoxHold);
        addFromCheckBox(whereElements,"aoe_hold",R.id.checkBoxAoeSlow);
        addFromCheckBox(whereElements,"hex",R.id.checkBoxHex);
        addFromCheckBox(whereElements,"silence",R.id.checkBoxSilence);
        addFromCheckBox(whereElements,"aoe_silence",R.id.checkBoxAoeSilence);
        addFromCheckBox(whereElements,"blind",R.id.checkBoxBlind);
        addFromCheckBox(whereElements,"aoe_blind",R.id.checkBoxAoeBlind);
        addFromCheckBox(whereElements,"aoe_dmg",R.id.checkBoxAoeDmg);
        addFromCheckBox(whereElements,"dot",R.id.checkBoxDot);
        addFromCheckBox(whereElements,"nuke",R.id.checkBoxNuke);
        addFromCheckBox(whereElements,"heal",R.id.checkBoxHeal);
        addFromCheckBox(whereElements,"protection",R.id.checkBoxProtection);
        addFromCheckBox(whereElements,"extended_dmg",R.id.checkBoxExtendedDmg);
        addFromCheckBox(whereElements,"crit",R.id.checkBoxCrit);
        addFromCheckBox(whereElements,"own_images",R.id.checkBoxOwnImages);
        addFromCheckBox(whereElements,"other_images",R.id.checkBoxOtherImages);
        addFromCheckBox(whereElements,"summons",R.id.checkBoxSummons);
        addFromCheckBox(whereElements,"take_control",R.id.checkBoxTakeControl);
        addFromCheckBox(whereElements,"blink",R.id.checkBoxBlink);
        addFromCheckBox(whereElements,"windwalk",R.id.checkBoxWindwalk);
        addFromCheckBox(whereElements,"manaburn",R.id.checkBoxManaburn);
        addFromCheckBox(whereElements,"tracking",R.id.checkBoxTracking);
        addFromCheckBox(whereElements,"minus_amor",R.id.checkBoxMinusAmor);
        addFromCheckBox(whereElements,"invisibility",R.id.checkBoxInvisibility);
        addFromCheckBox(whereElements,"bring_back",R.id.checkBoxBringBack);
        addFromCheckBox(whereElements,"purge",R.id.checkBoxPurge);
        addFromCheckBox(whereElements,"escapeskill",R.id.checkBoxEscape);
        addFromCheckBox(whereElements,"lifesteal",R.id.checkBoxLifeSteal);
    }

    private void handleRestrictions ( List<String> whereElements ) {
        addRestriction(whereElements,R.id.spinnerOrbs);        
        addRestriction(whereElements,R.id.spinnerBuffPlacers);        
    }

    private void addRestriction ( List<String> whereElements, int id ) {
        Spinner sp = (Spinner) findViewById(id);
        String sel = sp.getSelectedItem().toString();
        if ( "(all)".equals(sel) )
            return;
        else if ( "Yes".equals(sel) )
            whereElements.add("orb_effect = 1");        
        else 
            whereElements.add("orb_effect = 0");
    }

}
